package com.maserb.monitor.repository.impl;

import com.maserb.monitor.database.JdbcTemplateFactory;
import com.maserb.monitor.database.PreparedStatementUtils;
import com.maserb.monitor.entity.DeviceEntity;
import com.maserb.monitor.repository.DeviceRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DeviceRepositoryImpl implements DeviceRepository {
    @Autowired
    JdbcTemplateFactory jdbcTemplateFactory;

    @Override
    public void save(DeviceEntity entity) {
        JdbcTemplate jdbcTemplate = jdbcTemplateFactory.getTemplate(entity.getCode());

        String querySql = "select count(*) from device where id = ?";
        Integer count = jdbcTemplate.queryForObject(querySql, new Object[]{entity.getId()}, Integer.class);
        if (count > 0) {
            String updateSql = "update device d set d.name = ?,d.code = ?,d.longitude = ?,d.latitude = ?,d.image1 = ?," +
                    "d.image2 = ?,d.image3 =?,d.image4 = ?,d.custom = ?,d.description = ?,d.createTime = ?,d.category = ? where d.id = ?";

            prepareSaveParameters(entity, jdbcTemplate, updateSql);
        } else {
            String insertSql = "insert into device(name,code,longitude,latitude,image1,image2,image3,image4," +
                    "custom,description,createTime,category,id) values(?,?,?,?,?,?,?,?,?,?,?,?,?)";

            prepareSaveParameters(entity, jdbcTemplate, insertSql);
        }
    }


    @Override
    public DeviceEntity findOne(String id, String code) {
        JdbcTemplate jdbcTemplate = jdbcTemplateFactory.getTemplate(code);
        if (jdbcTemplate != null) {
            String findSql = "select * from device where id=?";
            List<DeviceEntity> ret = jdbcTemplate.query(findSql, new Object[]{id}, new BeanPropertyRowMapper<DeviceEntity>(DeviceEntity.class));
            if (ret != null && ret.size() > 0) {
                return ret.get(0);
            }
        }
        return null;
    }

    private void prepareSaveParameters(DeviceEntity entity, JdbcTemplate jdbcTemplate, String updateSql) {
        jdbcTemplate.update(updateSql, ps -> {
            PreparedStatementUtils.setString(ps, 1, entity.getName());
            PreparedStatementUtils.setString(ps, 2, entity.getCode());
            PreparedStatementUtils.setString(ps, 3, entity.getLongitude());
            PreparedStatementUtils.setString(ps, 4, entity.getLatitude());
            PreparedStatementUtils.setString(ps, 5, entity.getImage1());
            PreparedStatementUtils.setString(ps, 6, entity.getImage2());
            PreparedStatementUtils.setString(ps, 7, entity.getImage3());
            PreparedStatementUtils.setString(ps, 8, entity.getImage4());
            PreparedStatementUtils.setString(ps, 9, entity.getCustom());
            PreparedStatementUtils.setString(ps, 10, entity.getDescription());
            PreparedStatementUtils.setString(ps, 11, entity.getCreateTime());
            PreparedStatementUtils.setString(ps, 12, entity.getCategory());
            PreparedStatementUtils.setString(ps, 13, entity.getId());
        });
    }
}
